Welcome back.
In this lecture we're going to be implementing the animations we've just created into our guns.
So when we equip our gun we'll play the equip animation.
When we shoot our gun we play the shoot animation.
And when we reload we play the reload animation.
So let's get started.
You're most likely going to be using the animation rig that I've attached in the last lecture for your
gun animations, but if you've created your own gun animations, then that's great too.
Just make sure to open up your animation editor, select the rig and just go through and make sure you
have all of the different animations for our pistol, our shotguns, our rifle, and our SMG.
Make sure you have all of those animations uploaded.
Then you can go into the module scripts for each one of your guns, and you make sure to update the
animations with the corresponding IDs with the ones you've just uploaded.
So for example, if I go to my toolbox and I go to my animations, I can go ahead and search for example
for my shotgun and I've uploaded my shotgun animations, and then I've just copied the IDs and pasted
them into the modules for my shotguns.
I updated them right there, so make sure you have updated every single properties module script in
your guns to match the animations that you've uploaded.
Now we can go back into the local script that we've been editing so far.
I'll open it up and in order to load animations onto our player's character, we of course need to get
the character, the humanoid, and the animator inside of our humanoid.
So I'll create a variable for my character that's going to be equal to my player dot character or player
dot character added.
We'll wait for that event to fire just in case we'll get the humanoid.
So that's character.
Wait for child humanoid.
And then we can go ahead and get the animator.
So humanoid.
Wait for child animator.
Then we can go ahead and create a new function to create animation tracks.
So we'll call it Create Animation Track.
Again, we'll get past an animation ID and we'll simply create a new animation instance.
Set the animation ID equal to this anim id, and then we can go ahead and return from our animator.
And we're going to load this animation.
So then right here we can go ahead and create three variables one for our ship, one for our shoot and
one for our reload animation.
So we'll have the equip track that's going to be equal to create animation track.
And we're going to pass properties dot animations dot equip.
We can go ahead and get our shoot track.
And we'll just copy this here, paste that there, and then get the shoe animation.
And last but not least we'll copy this, paste that there.
And this one's going to be for our load track.
And we'll get the reload animation.
Now we can go ahead and go to our equip event.
And when we equip the gun, we want to play our equip animation.
So equip track play.
When we shoot our gun we want to play the shoot animation.
So after we do all the calculations for the gun at the end here in my shoot function, we can play the
shoot track.
And then for the reload function, what we want to do is we want to replace this yield statement here
with our animation.
So we're going to refer to our reload track, play this track.
And then we want to adjust the speed of this reload track to match inside of our properties the reload
duration that we've set here.
So reload track adjust speed.
And it's going to be one divided by our properties dot reload duration.
Then we can go ahead and wait for I believe, the ended event.
For this animation track, and we can go ahead and get rid of this yield statement.
And then once the animation track is finished playing, then we can go ahead and reload our gun.
And now what we want to go ahead and do is we want to check.
Or when we unequip our gun, we want to go ahead and stop playing all of the animations.
So if we were in the middle of a reload animation and we unequip the gun, we want to stop playing the
reload animation.
We want to stop playing the equip animation, and we want to stop playing the shoot animation.
So that's very easy.
We'll just refer to our shoot track and stop it.
We'll refer to our reload track and stop it, and we'll refer to our equip track and stop that as well.
Now, something actually very important to note is that if I go back to my animation rig, let me open
up my animation editor and I were to reload for the shoot track of my shotgun, let me find my shotgun
shoot animation.
So shotgun shoot animation.
I have added this event right here called pump.
It's an animation event, and we're going to use this animation event to play that shotgun pump sound
that was in the properties for our shotgun.
So for example, if we take a look at one of our shotguns here.
When we scroll down, we have the shotgun pump sound properties.
So for any of our guns that are shotguns, we're going to listen for this event inside of the animation
track.
And when we hit that event, we want to play that sound like we're wracking the shotgun.
If you haven't added this event into your shotgun shoot animation, then go ahead and do so.
You can just right click into your animation and you can press Add Animation Event.
And then you'll add it.
You'll give it a name, you'll call it pump, and that's it.
That's all you need to do.
You'll have your new animation track inside of your animation, and then you can go ahead and upload
it to Roblox.
So if you've already uploaded your shotgun shoe animation but you did not add this event, then go ahead
and add this event into your shotgun shoe animation, and then make sure to publish and override the
one you've previously uploaded.
However, if you're using my animations, then you don't need to worry because my animations will already
have this event included in the animation.
So back in our local script, what we want to do is we want to check if our gun is a shotgun.
So if gun get attribute gun type is equal to the spread or shotgun gun type, then what we can go ahead
and do is refer to our shoot animation track.
And we're going to use a function called get marker reach signal.
Now the linter does not know what our shoot track variable is.
So just to make it a little bit more clear, I'm going to signify that this function right here returns
an animation track.
Again, you do not need to worry about this type annotation.
I'm just doing it for the lecture to make it easier for us to code, because now it will auto fill all
of the stuff for my animation track.
And the function we're looking for is the get marker reach signal.
So this is where we pass the name of that event, which would be pump.
And that means we're going to get an event that will be fired when we reach that event in our animation.
So our event is called pump.
And we can go ahead and connect a function to this.
And what we want to do is we want to create a new sound from our create sound function.
And that sound is going to be parented to our gun dot handle.
And the properties is going to be the shotgun.
So shotgun pump sound properties.
Then we can go ahead and play the sound.
And then when that sound ends, we'll go ahead and destroy the sound.
And just like that, we should now have implemented all the animations into our gun.
So to test this out, we can.
Go and copy our assault rifle and paste it into the starter pack.
We can also copy one of our shotguns and paste it in there.
And let me make sure to put the newly updated local script in there and get rid of the old one.
And then we could test another gun like our pistol.
So I'll copy our pistol, paste that in there, and I'll make sure to copy the local script inside of
it as well.
And now we can go and play test our game.
Now let me spawn in.
And now if I equip my guns.
As you can see, each of them has their equip animation track plane.
If I shoot, as you can see, it's playing the shoot animation track and if I hold down, as you can
see it is repeatedly playing that animation.
Same thing with the shotgun if I shoot and there you go, you heard the pump sound because when I click,
it reached that animation event inside of our animation and it's playing the racking sound for the shotgun.
Very cool.
And then for the pistol, let's go ahead and try it out.
All right.
That's working as well.
Let's try reloading.
So if I hit R.
Here we go.
Our reload track plays as well and we've reloaded our pistol.
Let's go ahead and try it out for the shotgun.
There you go.
We're reloading our shotgun, and that works as well.
Let's try the AR.
Reload.
And.
Perfect.
It's working just fine.
And just like that, we're finished implementing the animations for our guns.
I'll see you in the next lecture.